/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package huffmantree;

/**
 *
 * @author Administrator
 */
public class BinaryTree <E>{

    protected E value;
    protected BinaryTree<E> parent, left, right;

    public BinaryTree(){

        value = null;
        parent = null;
        left = null;
        right = null;
    }

    public BinaryTree(E val){
        
        value = val;
        right = left =  new BinaryTree<E>();
        left = null;
        right = null;
        //setLeft(left);
        //setRight(right);
    }

    public BinaryTree(E val, BinaryTree<E> left, BinaryTree<E> right){

        value = val;
        if (left == null)
            left = new BinaryTree<E>();
        setLeft(left);
        if (right == null)
            right = new BinaryTree<E>();
        setRight(right);
    }

    protected void setParent(BinaryTree<E> p){
        parent = p;
    }

    public void setLeft(BinaryTree<E> l){
        if (left != null && left.getParent() == this)
            left.setParent(null);
        left = l;
        left.setParent(this);
    }

    public void setRight(BinaryTree<E> r){
        if (right != null && right.getParent() == this)
            right.setParent(null);
        right = r;
        right.setParent(this);
    }

    public void setValue(E val){
        value = val;
    }

    protected BinaryTree<E> getParent(){
        return(this.parent);
    }

    public BinaryTree<E> getLeft(){
        return(left);
    }

    public BinaryTree<E> getRight(){
        return(right);
    }

    public E getValue(){
        return(value);
    }
}